package ${packageName}.controller;

import BeanUtil;
import DateUtil;
import cn.com.gc.core.excel.ColumnBox;
import cn.com.gc.core.excel.ExportUtils;
import cn.com.gc.core.mybatis.PageData;
import cn.com.gc.frame.enums.EnableStatusEnum;
import cn.com.gc.frame.enums.StatusEnum;
import cn.com.gc.frame.enums.SysMsgEnumType;
import cn.com.gc.hsz.admin.frame.config.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import ${packageName}.vo.valid.${moduleUpper}ValidGroup;
import ${packageName}.service.${name}WebService;
import ${packageName}.model.${name};
import ${packageName}.vo.request.${name}RequestVo;
import ${packageName}.vo.response.${name}ResponseVo;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**   
 * @Title: ${name}WebController.java
 * @Description: ${description}的控制服务类
 * @author ${author}
 * @date   ${newdate}
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *${description}
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/${module}/${nameLower}")
public class ${name}WebController {
    private final static Logger logger = LoggerFactory.getLogger(${name}WebController.class);
	@Autowired
	private ${name}WebService  ${nameLower}WebService;
    /**
     * @Title: 跳转到${description}主界面
     * @Description:${nameLower}RequestVo @{Link ${name}RequestVo}
     * @return
     * @throw
     * @author ${author}
     * @date ${newdate}
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    @RequiresPermissions("${module}:${nameLower}_list")
    public String index(){
        return "${module}/${nameLower}_index";
    }
    /**
     * @Title: 获取${description}分页信息
     * @Description:${nameLower}RequestVo @{Link ${name}RequestVo}
     * @param
     * @return
     * @throw
     * @author ${author}
     * @date ${newdate}
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    @RequiresPermissions("${module}:${nameLower}_list")
    public PageData<${name}ResponseVo> list(@Validated(value = {${moduleUpper}ValidGroup.${moduleUpper}QueryGroup.class}) ${name}RequestVo ${nameLower}RequestVo){
        ${name} ${nameLower} = new ${name}();
        BeanUtil.copyProperties(${nameLower}RequestVo, ${nameLower});
        return ${nameLower}WebService.get${name}Page(${nameLower}, ${nameLower}RequestVo.getPage(), ${nameLower}RequestVo.getLimit());
    }
   /**
    * @Title: 新增${description}信息
    * @Description:${nameLower}RequestVo @{Link ${name}RequestVo}
    * @param
    * @return
    * @throw
    * @author ${author}
    * @date ${newdate}
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "add")
    @RequiresPermissions("${module}:${nameLower}_add")
    public ResponseResult add(@Validated(value = {${moduleUpper}ValidGroup.${moduleUpper}FormGroup.class}) ${name}RequestVo ${nameLower}RequestVo){
        ${name} ${nameLower} = new ${name}();
        BeanUtil.copyProperties(${nameLower}RequestVo, ${nameLower});
        ${nameLower}.setStatus(StatusEnum.VALID.getIndex());
        ${nameLower}.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
        ${nameLower}WebService.add${name}(${nameLower});
        return ResponseResult.ok();
    }
    /**
    * @Title: 修改${description}信息
    * @Description:${nameLower}RequestVo @{Link ${name}RequestVo}
    * @param
    * @return
    * @throw
    * @author ${author}
    * @date ${newdate}
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "modify")
    @RequiresPermissions("${module}:${nameLower}_modify")
    public ResponseResult modify(@Validated(value = {${moduleUpper}ValidGroup.${moduleUpper}FormGroup.class}) ${name}RequestVo ${nameLower}RequestVo){
        ${name} ${nameLower} = new ${name}();
        BeanUtil.copyProperties(${nameLower}RequestVo, ${nameLower});
        if(${nameLower}.get${pkUpper}() == null){
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_UPD_INFO_NO_EXIST);
        }
        ${nameLower}WebService.upd${name}(${nameLower});
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看${description}信息
    * @Description:${nameLower}RequestVo @{Link ${name}RequestVo}
    * @param
    * @return
    * @throw
    * @author ${author}
    * @date ${newdate}
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    @RequiresPermissions("${module}:${nameLower}_list")
    public String detail(ModelMap modelMap, ${pkType} id){
        ${name} ${nameLower} = null;
        if(${pkCheck}){
            ${nameLower} = ${nameLower}WebService.get${name}(id);
        }
        modelMap.put("${nameLower}", ${nameLower});
        return "${module}/${nameLower}_detail";
    }
    /**
     * @Title: 跳转${description}编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author ${author}
     * @date ${newdate}
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    @RequiresPermissions("${module}:${nameLower}_modify")
    public String edit(ModelMap modelMap, @RequestParam${pkDefault} ${pkType} id){
        ${name} ${nameLower} = null;
        if(${pkCheck}){
            ${nameLower} = ${nameLower}WebService.get${name}(id);
        }
        modelMap.put("${nameLower}", ${nameLower});
        return "${module}/${nameLower}_edit";
    }
    /**
    * @Title: 跳转${description}新增编辑界面
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author ${author}
    * @date ${newdate}
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "newInfo")
    @RequiresPermissions("${module}:${nameLower}_add")
    public String newInfo(ModelMap modelMap){
        return "${module}/${nameLower}_edit";
    }
    /**
     * @Title: 删除${description}信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author ${author}
     * @date ${newdate}
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    @RequiresPermissions("${module}:${nameLower}_del")
    public ResponseResult del( @RequestParam(value = "id") ${pkType}[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        ${nameLower}WebService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用${description}信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author ${author}
    * @date ${newdate}
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    @RequiresPermissions("${module}:${nameLower}_turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") ${pkType}[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        ${nameLower}WebService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用${description}信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author ${author}
    * @date ${newdate}
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    @RequiresPermissions("${module}:${nameLower}_turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") ${pkType}[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        ${nameLower}WebService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出${description}信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author ${author}
    * @date ${newdate}
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    @RequiresPermissions("${module}:${nameLower}_export")
    public void export(HttpServletResponse response, ${name}RequestVo ${nameLower}RequestVo){
        ${name} ${nameLower} = new ${name}();
        BeanUtil.copyProperties(${nameLower}RequestVo, ${nameLower});
        PageData<${name}ResponseVo> ${nameLower}Page = ${nameLower}WebService.get${name}Page(${nameLower}, ${nameLower}RequestVo.getPage(), ${nameLower}RequestVo.getLimit());
        List<${name}ResponseVo> data = ${nameLower}Page.getData();

        ColumnBox columnBox = ColumnBox.create()
                ${columnBox};

        try {
            ExportUtils.write(String.format("${description}_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}